#include<iostream>
#include<cstdio>
#include<stdlib.h>
using namespace std;
#define ElemType int
typedef struct LNode{
	ElemType data;
	struct LNode *next;
}LNode,*LinkList;
LNode* InitList(LinkList L)
{
	cout<<"Initialize the Single-linked list "<<'\n'; //初始化链表
	L=new LNode;
	L->next=NULL;
	cout<<"Initialization succeeded"<<'\n'; //初始化成功
	return L;
}
void InsertList(LinkList L,ElemType a,ElemType b,ElemType c,ElemType d,ElemType e)
{
	LNode *p1=new LNode;
	LNode *p2=new LNode;
	LNode *p3=new LNode;
	LNode *p4=new LNode;
	LNode *p5=new LNode;
	p1->data=a;
	p1->next=L->next;
	L->next=p1;
	
	p2->data=b;
	p2->next=p1->next;
	p1->next=p2;
	
	p3->data=c;
	p3->next=p2->next;
	p2->next=p3;
	
	p4->data=d;
	p4->next=p3->next;
	p3->next=p4;
	
	p5->data=e;
	p5->next=p4->next;
	p4->next=p5;

	
}
void DispList(LinkList L)
{
	
	LNode *p=new LNode;
	p=L->next;
	while(p)
	{
		cout<<p->data<<" ";
		p=p->next;
	}
	cout<<'\n';

}
int ListLength(LinkList L)
{
	LNode *p=new LNode;
	p=L->next;
	int a=0;
	while (p)
	{
		a++;
		p=p->next;
	}
	return a;

}
bool Empty(LinkList L)
{
	if(L->next)
	return 1;
	else
	return 0;
}
void GetElem(LinkList L,int a)
{
	LNode *p=new LNode;
	p=L->next;
	int j=1;
	while (p&&j++!=a)
	{
	p=p->next;
	}
	cout<<"The element is "<<p->data<<'\n';//该元素为

}
void GetPos(LinkList L,ElemType d)
{
	LNode* p=new LNode;
	int i=1;
	p=L->next;
	while(p&&p->data!=d)
	{
		p=p->next;
		i++;
	}
	printf("The position of element %d is in the %d\n",d,i);//元素d的位置在第几个
	
}
void ListInsertP(LinkList L,ElemType e)
{
	LNode *p1=new LNode;
	p1=L->next;
	int i=1;
	while(p1&&i++!=3)
	{
		p1=p1->next;
	}
	LNode *p=new LNode;
	p->data=e;
	p->next=p1->next;
	p1->next=p;

}
void ListDelete(LinkList L,int a)
{
	LNode *p=new LNode;
	p=L->next;
	int i=1;
	while(p&&i++!=a-1)
	{
		p=p->next;
	}
	p->next=p->next->next;

}
void ListReverse(LinkList L)
{
	ElemType e=1,t;
	LNode* p=new LNode;
	LNode* q=new LNode;
	LNode* o=new LNode;
	p=L->next;
	o=L->next;
	int i,x=ListLength(L)/2,n=1,m=ListLength(L);
	for(i=0;i<x;i++)
	{
		while(e++!=m)
		{
			if(e-1==n)
			{
				t=p->data;
				q=p;
			}
			p=p->next;
		
		}
		if(e-1==m)
		{
			q->data=p->data;
			p->data=t;
		}		
		p=o->next;
		o=o->next;
		m--;
		n++;
		e=n;
	}
	
}
int main()
{
	ElemType a,b,c,d,e,n;
	char f;
	LinkList L;
	L=InitList(L);
	cout<<"Enter the five elements of the trailing insert"<<'\n'; //输入五个尾插的元素
	cin>>a>>b>>c>>d>>e;
	InsertList(L,a,b,c,d,e);
	cout<<"Output a Single-linked list "<<'\n';        //输出单链表        
	DispList(L);
	cout<<"Single-linked list length is "<<ListLength(L)<<'\n';         //单链表的长度是
	
	if(Empty(L))
	cout<<"The Single-linked list is not empty"<<'\n';//单链表不为空
	else
	cout<<"The single-linked list is empty"<<'\n';//单链表为空
	cout<<"Please enter 'T' to output the third element of the singly linked list"<<'\n';//输入字符'T'来输出单链表第三个元素
	cin>>f;
	GetElem(L,3);
	cout<<"Enter element d to output the position of element d"<<'\n';//输入元素d来输出元素d的位置
	cin>>n;
	GetPos(L,n);
	cout<<"Enter the element inserted at the fourth position"<<'\n';//输入在第四个位置上插入的元素
	cin>>e;
	ListInsertP(L,e);
	cout<<"Output a Single-linked list "<<'\n';                   //输出单链表  
	DispList(L);
	cout<<"Please enter 'D' to delete the second element of the singly linked list"<<'\n';//输入字符'D'来输出单链表第二个元素
	cin>>f;
	ListDelete(L,2);
	cout<<"Output a Single-linked list "<<'\n';                   //输出单链表  
	DispList(L);
	cout<<"Enter the character 'N' to reverse the singly linked list"<<'\n';
	cin>>f;
	ListReverse(L);
	cout<<"Output a Single-linked list "<<'\n';                   //输出单链表  
	DispList(L);
	cout<<"experiment over"<<'\n';
	return 0;
}